package com.cicadalane.androlate;

/*
 * Copyright (C) 2011 cicada.software@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.net.URLEncoder;
import java.util.ArrayList;

public class GoogleTranslateParameters {
	private String[] query_ = null;
	private String[] from_ = null;
	private String[] to_ = null;
	private String callback_ = null;
	private String context_ = null;
	private static final String version_ = "1.0";
	private String userIp_ = null;
	private String hostLanguage_ = null;
	private String apiKey_ = null;

	public GoogleTranslateParameters(String query, String from, String to,
			String userIp, String hostLanguage, String apiKey, String callback,
			String context) {
		query_ = new String[] { query };
		from_ = new String[] { from };
		to_ = new String[] { to };
		userIp_ = userIp;
		hostLanguage_ = hostLanguage;
		apiKey_ = apiKey;
		callback_ = callback;
		context_ = context;
	}

	public GoogleTranslateParameters(String query, String from, String to,
			String userIp, String hostLanguage, String apiKey, String callback) {
		query_ = new String[] { query };
		from_ = new String[] { from };
		to_ = new String[] { to };
		userIp_ = userIp;
		hostLanguage_ = hostLanguage;
		apiKey_ = apiKey;
		callback_ = callback;
		context_ = null;
	}

	public GoogleTranslateParameters(String query, String from, String to,
			String userIp, String hostLanguage, String apiKey) {
		query_ = new String[] { query };
		from_ = new String[] { from };
		to_ = new String[] { to };
		userIp_ = userIp;
		hostLanguage_ = hostLanguage;
		apiKey_ = apiKey;
		callback_ = null;
		context_ = null;
	}

	public GoogleTranslateParameters(String query, String from, String to,
			String userIp, String hostLanguage) {
		query_ = new String[] { query };
		from_ = new String[] { from };
		to_ = new String[] { to };
		userIp_ = userIp;
		hostLanguage_ = hostLanguage;
		apiKey_ = null;
		callback_ = null;
		context_ = null;
	}

	public GoogleTranslateParameters(String query, String from, String to,
			String userIp) {
		query_ = new String[] { query };
		from_ = new String[] { from };
		to_ = new String[] { to };
		userIp_ = userIp;
		hostLanguage_ = null;
		apiKey_ = null;
		callback_ = null;
		context_ = null;
	}

	public GoogleTranslateParameters(String query, String from, String to) {
		query_ = new String[] { query };
		from_ = new String[] { from };
		to_ = new String[] { to };
		userIp_ = null;
		hostLanguage_ = null;
		apiKey_ = null;
		callback_ = null;
		context_ = null;
	}

	public GoogleTranslateParameters(String[] query, String[] from, String[] to, String userIp) {
		query_ = query;
		from_ = from;
		to_ = to;
		userIp_ = userIp;
		hostLanguage_ = null;
		apiKey_ = null;
		callback_ = null;
		context_ = null;
	}
	
	public String urlEncode() {
		String result = null;
		ArrayList<String> params = new ArrayList<String>();

		int length = query_.length;
		for (int i = 0 ; i < length; i++) {
			params.add("langpair=" + from_[i] + "%7C" + to_[i]);
			if (query_[i] != null && query_[i].length() > 0) {
				String encodedString;
				try {
					encodedString = URLEncoder.encode(query_[i], "UTF-8");
				} catch (Exception ex) {
					encodedString = query_[i];
				}
				params.add("q=" + encodedString);
			}
		}

		try {
			result = StringUtils.join(params, '&');
			result += "&v=1.0";
			result += "&userip=" + userIp_;
		} catch (Exception e) {
			result = null;
		}
		return result;
	}
}
